import 'package:aichat_flutter_wallet/module/module_browser/viewmodel/we3_method.dart';
import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/dialog/bottom_sheet_widget.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/image/public_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';

class DappSelectChainWidget extends StatefulWidget {
  String walletShortName;
  String? title;
  DappSelectChainWidget({super.key, this.title, required this.walletShortName});

  @override
  State<DappSelectChainWidget> createState() => _DappSelectChainWidgetState();
}

class _DappSelectChainWidgetState extends State<DappSelectChainWidget> {
  List<Chain> netWorkList = Web3MethodCall.getNetworkList();
  int curIndex = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    doInit();
  }

  setCurIndex(int index) {
    setState(() {
      curIndex = index;
      Chain item = netWorkList[index];
      Navigator.of(context).pop(item);
    });
  }

  doInit() {
    int index = netWorkList.indexWhere((element) {
      return element.shortName == widget.walletShortName;
    });

    if (index != -1) {
      setState(() {
        curIndex = index;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return BottomSheetWidget(
      title: widget.title ?? 'Select Network',
      expand: false,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              height: 430.w,
              child: ListView.builder(
                  itemCount: netWorkList.length,
                  itemBuilder: (_, index) {
                    return InkWell(
                      onTap: () {
                        setCurIndex(index);
                      },
                      child: Container(
                        color: index == curIndex ? AppColors.c_F6F6F6 : AppColors.c_white,
                        width: double.infinity,
                        height: 60.w,
                        padding: EdgeInsets.symmetric(horizontal: 20.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                ClipRRect(
                                  borderRadius: BorderRadius.circular(40),
                                  child: PublicImage(
                                    image: netWorkList[index].logo,
                                    width: 40.w,
                                    height: 40.w,
                                  ),
                                ),
                                SizedBox(
                                  width: 10.w,
                                ),
                                Text(
                                  netWorkList[index].name,
                                  style: TextStyle(
                                      fontSize: 14.sp, color: AppColors.c_333333, fontWeight: FontWeight.w500),
                                )
                              ],
                            ),
                            Visibility(
                                visible: index == curIndex,
                                child: Image.asset(
                                  'assets/images/wallet_assets/network_selected.png',
                                  width: 24.w,
                                  height: 24.w,
                                ))
                          ],
                        ),
                      ),
                    );
                  }),
            ),
            SizedBox(
              height: 10 + MediaQuery.of(context).padding.bottom,
            ),
          ],
        ),
      ),
    );
  }
}
